﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.ComponentModel;
using Skynet.BusinessEntities;
using Skynet.WebFoundation.Services.Navigation;
using Skynet.WebFoundation.Services.Session;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using System.Xml;
using System.Xml.Linq;
using Infrastructure.Aspect.Services.XmlDataQueries;
using Skynet.BusinessLogic.Constants;
using System.Web;
using Infrastructure.Aspect.ExceptionHandling;
using System.Data.SqlClient;
using System.Threading;
using Skynet.BusinessLogic.BusinessComponents;


namespace Skynet.WebAdministracion.Views.ExamsMgm
{
    [System.ComponentModel.DataObject]
    public class ExamsMgmPresenter : Infrastructure.Web.Views.Master.MasterPresenter<IExamsMgmView>
    {
        
        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExams BCExams { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExamQueries BCExamQueries { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCCustomers BCCustomers { get; set; }

        [Dependency]
        public IBCCustomersQueries BCCustomersQueries { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCReports BCReports { get; set; }

        [Dependency]
        public INavigationService NavigationService { get; set; }

        [Dependency]
        public ISessionService SessionService { get; set; }
        [Dependency]
        public IXmlDataQueriesService XmlDataQueriesService { get; set; }

        public override void OnViewLoaded()
        {            
            
        }
        public override void OnViewInitialized()
        {
            this.SessionService.CustomerId = this.BCCustomersQueries.FindCustomers(option: BusinessLogic.BusinessComponents.CustomerFindOptions.GetByUserName, UserName: System.Web.HttpContext.Current.User.Identity.Name).SingleOrDefault().Id;
        }

        [DataObjectMethod(DataObjectMethodType.Insert, false)]
        public void AddExamen(Exam item)
        {
            item.Id = Guid.NewGuid();
            item.MarkAsAdded();
            this.BCExams.MaintenanceExams(item);
            this.NavigationService.NavigateRoute(Constants.RoutesNames.DesignAdministration);
        }
        [DataObjectMethod(DataObjectMethodType.Update, false)]
        public void UpdateExamen(Exam item)
        {
            this.BCExams.MaintenanceExams(item);
        }
        [DataObjectMethod(DataObjectMethodType.Delete, false)]
        public void DeleteExam(Exam item)
        {
            try
            {
                var exam = this.GetExam(item.Id);
                if (exam != null)
                {
                    exam.MarkAsDeleted();
                    this.BCExams.MaintenanceExams(exam);
                }
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, Infrastructure.Aspect.ExceptionHandling.ExceptionPolicyNames.WebPresenterPolicy);
                if ( rethrow )
                    throw;
            }                            
        }

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public IEnumerable<ExamCategory> GetCategorias()
        {
            return this.BCExamQueries.SearchExamCategories();
        }
        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public IEnumerable<object> GetExamenes(Guid CategoryId)
        {
            IEnumerable<object> result = null;
            try
            {

                Thread.Sleep(2000);
                var target = this.BCExamQueries.SearchExamCategories(BusinessLogic.BusinessComponents.SearchExamCategoriesOptions.GetAll );
                var query = from c in target   select new { Id = c.Id, Title = c.Title, Summary = c.Description };
                result = query.ToList();
            }
            finally { 

            }
            //    var res = this.BCExamQueries.QueryExams(Options: BusinessLogic.BusinessComponents.QueryExamOptions.DesignListView, CategoryId: CategoryId, CustomerId : SessionService.CustomerId);

            //    if (!string.IsNullOrEmpty(res))
            //    {
            //        var query = from c in XElement.Parse(res).Descendants("Exam")
            //                    select new { Id = Guid.Parse(c.Element("Id").Value), 
            //                        Title = (string)c.Element("Title"), 
            //                        Summary = (string)c.Element("Summary") };
            //        result = query.ToList();
            //    }
            //    else
            //    {
            //        result = new List<object>();
            //    }                
            //}
            //catch (ApplicationException ex)
            //{
            //    this.View.SupportMessage = ex.Message;
            //}            
            //catch (Exception ex)
            //{
            //    bool rethrow = ExceptionPolicy.HandleException(ex, ExceptionPolicyNames.WebPresenterPolicy);
            //    if (rethrow)
            //        throw;
            //}                     
            return result;
        }
        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public Exam GetExam(Guid Id)
        {
            if ( Id == Guid.Empty) return null;
            var exam = this.BCExamQueries.SearchExams(BusinessLogic.BusinessComponents.ExamSearchOptions.GetOne,  Id).SingleOrDefault();
            return exam;
        }

        public void SelectExam(Guid ExamId)
        {
            this.SessionService.DesignExamId = ExamId;
            this.SessionService.DesignCategoryExamId = this.View.CategoryId;
            this.NavigationService.NavigateRoute(Constants.RoutesNames.DesignExam);
        }
        public void NewExam()
        {            
            this.SessionService.DesignExamId = null;
            this.SessionService.DesignCategoryExamId = this.View.CategoryId;
            this.NavigationService.NavigateRoute(Constants.RoutesNames.DesignExam);
        }        
    }
}
